#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/6/10 13:50
# @Author  : fuchen'en
# @Description:
import pika

credentials = pika.PlainCredentials('test', 'test123')
parameters = pika.ConnectionParameters(host='10.0.20.66',
                                       port='5672',
                                       virtual_host='/',
                                       credentials=credentials)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()

# 由于不知道是生产者先运行还是消费者先运行，所以需要重复声明队列，声明多次同一名称队列，只会生成一个
channel.queue_declare(queue='hello')


# 当获取到消息时，pika库会调用这个回调函数
def callback(ch, method, properties, body):
    print(" [x] Received {}".format(body))


# 告诉RabbitMQ这个回调函数会从名为“hello”的队列中接收消息
channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')

# 等待接收消息
channel.start_consuming()
